
package com.blank.task07;

import java.util.Arrays;
import java.util.Random;

public class ArrayComputationTry {
    public static void main(String[] args) {
        //声明一个16行16列二维数组用来存放数值
        int[][] arr = new int[16][16];
        //双重for循环进行随机赋值
        for(int i=0;i<arr.length;i++){
            for(int j=0;j<arr[0].length;j++){
                Random ran = new Random();
                //0~99的随机值
                int num = ran.nextInt(100);
                arr[i][j] = num;
                System.out.print(arr[i][j]+" ");

            }
            System.out.println();
        }
        //声明一个一维数组存放每一列的值
        int[] colArr = new int[16];
        //声明一个一维数组存放每一行的值
        int[] rowArr = new int[16];
        for(int i=0;i<arr.length;i++){
            //声明一个变量记录每一行的数值
            int sumOfCol = 0;
            int sumOfRow = 0;
            for(int j=0;j<arr[0].length;j++){
                sumOfCol += arr[i][j];
                sumOfRow += arr[j][i];
            }
            colArr[i] = sumOfCol;
            rowArr[i] = sumOfRow;
        }
        System.out.println("每一行的累加和为:");
        System.out.println(Arrays.toString(colArr));
        System.out.println("每一列的累加和为:");
        System.out.println(Arrays.toString(rowArr));
        //声明一个一维数组存放从右上角到左下角的数值
        int[] leftDiagonalArr = new int[16];
        //声明一个一维数组存放从左上角到右下角的数值
        int[] RightDiagonalArr = new int[16];
            for(int i=0;i<arr.length;i++){
                int LeftDiagonal = 0;
                int RightDiagonal = 0;
                for(int j=0;j<arr[0].length;j++){
                   if(i == j){
                       RightDiagonal = arr[i][j];
                   }
                   LeftDiagonal = arr[i][arr[0].length-1-i];
                }
                leftDiagonalArr[i] = LeftDiagonal;
                RightDiagonalArr[i] = RightDiagonal;
            }
        System.out.println("从左上角到右下角的数值为:");
        System.out.println(Arrays.toString(RightDiagonalArr));
        System.out.println("从右上角到左下角的数值为:");
        System.out.println(Arrays.toString(leftDiagonalArr));
        System.out.println("每一行的累加和为:"+countSum(colArr));
        System.out.println("每一列的累加和为:"+countSum(rowArr));
        System.out.println("从左上角到右下角的数值为:"+countSum(leftDiagonalArr));
        System.out.println("从右上角到左下角的数值为:"+countSum(RightDiagonalArr));
    }
    //定义一个方法计算一维数组之和
    public static int countSum(int[] array){
        int sum = 0;
        for(int i=0;i<array.length;i++){
            sum += array[i];
        }
        return sum;
    }

}
